package com.app.controller;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.app.entity.*;
import com.app.service.*;
import com.app.utils.MD5;
import com.app.utils.MainUtils;

@Controller
@RequestMapping("/users")
public class UsersController {
    @Resource
    UsersService usersService;

    @Resource
    InfocateService infocateService;
    @RequestMapping("/users/add")
    public String usersAdd(Model model) {

model.addAttribute("list",infocateService.getAllInfocate() );
        return "admin/users/add";
    }

    @RequestMapping(value = "/users/save", method = RequestMethod.POST)
    public String usersSave(String username, String password, String sex,
                            String birthday, String address, String email, String qq,
                            String tel, String money, String thumb, String grade, String type,
                            String created, String updated, String status, String age,
                            String ecard, String fromid, String fromname, String company,
                            String realname,
                            String position, String signature, Model model) {
        Users modelX = new Users();
        modelX.setUsername(username);
        modelX.setPassword(MD5.MD51(password));
        modelX.setSex(sex);
        modelX.setBirthday(birthday);
        modelX.setAddress(address);
        modelX.setEmail(email);
        modelX.setQq(qq);
        modelX.setTel(tel);
        modelX.setMoney(money);
        modelX.setThumb(thumb);
        modelX.setGrade(grade);
        modelX.setType(type);
        modelX.setCreated(MainUtils.getTime());
        modelX.setAge(age);
        modelX.setEcard(ecard);
        modelX.setFromid(fromid);
        modelX.setFromname(fromname);
        modelX.setCompany(company);
        modelX.setRealname(realname);
        modelX.setPosition(position);
        modelX.setSignature(signature);
        modelX.setMoney(money);
        
        int id = usersService.insert(modelX);
        Users user = usersService.getUsersById(id);
        System.out.println("插入ID"+id);
        Ordertab modelS = new Ordertab();
		modelS.setOrder_id(MainUtils.getOrderIdByUUId());
		modelS.setBuyer(username);
		modelS.setCreated(MainUtils.getTime());
		modelS.setPid(id + "");
		modelS.setUid(usersService.getAllUsers().get(0).getId()+ "");
		modelS.setPname("职工-");
		modelS.setPrice("100");
		modelS.setNum("1");
		modelS.setStep("1");

		ordertabService.insert(modelS);

        model.addAttribute("inpost", true);
        model.addAttribute("go", "users/users/list");
        model.addAttribute("message", "Success!");
        return "admin/users/add";
    }

    @RequestMapping("/users/delete")
    public String usersDelete(int id, Model model) {
        usersService.delete(id);
        model.addAttribute("message", "Success!");
        model.addAttribute("go", "users/users/list");
        return "admin/users/list";
    }
    @Resource
    OrdertabService ordertabService;

    @RequestMapping("/users/profile")
    public String userprofile(String id, HttpSession session, Model model) {
        int userId;
        if (id == null) {
            userId = Integer.parseInt(session.getAttribute("userid") + "");
        } else {
            userId = Integer.parseInt(id);
        }
        model.addAttribute("list",infocateService.getAllInfocate() );
        Users user = usersService.getUsersById(userId);
        model.addAttribute("user", user);
        return "admin/users/profile";
    }

    @RequestMapping("/users/password")
    public String userpassword(String id, HttpSession session, Model model) {
        int userId;
        if (id == null) {
            userId = Integer.parseInt(session.getAttribute("userid") + "");
        } else {
            userId = Integer.parseInt(id);
        }

        Users user = usersService.getUsersById(userId);
        model.addAttribute("user", user);
        return "admin/users/password";
    }

    @RequestMapping(value = "/users/update", method = RequestMethod.POST)
    public String usersUpdate(String id, String username, String password,
                              String sex, String birthday, String address, String email,
                              String qq, String tel, String money, String thumb, String grade,
                              String type, String created, String updated, String status,
                              String realname, String age, String ecard, String position,
String company,
                              Model model) {

        Users modelX = usersService.getUsersById(Integer.parseInt(id));
        modelX.setUsername(username);
        modelX.setAddress(address);
        modelX.setEmail(email);
        modelX.setQq(qq);
        modelX.setTel(tel);
        modelX.setThumb(thumb);
        modelX.setEcard(ecard);
        modelX.setAge(age);
        modelX.setMoney(money);
        modelX.setRealname(realname);
        modelX.setSex(sex);
        modelX.setCompany(company);
        modelX.setPosition(position);
        usersService.update(modelX);
        model.addAttribute("inpost", true);
        model.addAttribute("message", "更新成功");
        model.addAttribute("user", modelX);
        return "admin/users/profile";
    }

    @RequestMapping(value = "/users/updatepassword", method = RequestMethod.POST)
    public ModelAndView updatepassword(int id, String password, Model model) {
        Users user = usersService.getUsersById(id);
        user.setPassword(MD5.MD51(password));
        usersService.update(user);
        model.addAttribute("user", user);
        ModelAndView mav = new ModelAndView("admin/users/password");
        mav.addObject("message", "更新成功");

        return mav;
        // return "redirect:profile?id="+id;
    }

    @RequestMapping("/users/list")
    public ModelAndView usersList(String type) {
        List<Users> usersList = usersService.getAllUsers();
        List<Users> usersList1 = new LinkedList<Users>();

        if (usersList.size() > 0) {
            for (Users users : usersList) {
            	if(type!=null){
            		//if(!type.equals(users.getType())) continue;
            	}
            	if(users.getCompany()!=null){
            		users.setCompany(
            		infocateService.getInfocateById(Integer.parseInt(users.getCompany()))
            		.getTitle());
            		
            	}
            	
            	
            	
                usersList1.add(users);
            }
        }

        System.out.println(usersList.size());
        ModelAndView mav = new ModelAndView("admin/users/list");
        mav.addObject("list", usersList1);
        return mav;
    }

    @RequestMapping("/users/list1")
    public ModelAndView usersList1() {
        List<Users> usersList = usersService.getAllUsers();
        List<Users> usersList1 = new LinkedList<Users>();

        if (usersList.size() > 0) {
            for (Users users : usersList) {
                usersList1.add(users);
            }
        }

        System.out.println(usersList.size());
        ModelAndView mav = new ModelAndView("admin/users/list1");
        mav.addObject("list", usersList1);
        return mav;
    }

    @RequestMapping("/users/edit")
    public String usersEdit(String id, Model model) {
        Users users = usersService.getUsersById(Integer.parseInt(id));
        model.addAttribute("model", users);
        return "admin/users/edit";
    }

}
